Raphael.Chart.line = Raphael.Chart.line || {};

Raphael.Chart.line.theme = {
              title : {
                    title : {style:{fill:"white","font-size":30}},
                    subtitle:{ style:{fill:"#aaa","font-size":12}}
                  },
              grid : {
                    display : true,
                    lines : {stroke : "white"},
                    interLines : {display : true,
                            style : { stroke : "white" , "stroke-width" : 0.1}
                            },
                    animation : {
                        animateIt : true,
                        ms : 2000,
                        style : "bounce"
                      }
                  },
              xaxis : {
                    display : true,
                    arrow : {display : true,double : false,width : 7,height:7,style:{fill:"white"}},
                    line : {stroke:"white","stroke-width":2} // style of the line
                  },
              yaxis : {
                    display : true,
                    arrow : {display : true,double : false,width : 7,height:7,style:{fill:"white"}},
                    line : {stroke:"white","stroke-width":2} // style of the line
                  },
              transparentRect : {
                        display : true,
                        color : "#fff",
                        opacity : 0.7,
                        radius : 0
                        },
              background : {
                      display : true,
                      type : "color",
                      color : "45-#f00-#000",
                      "img-src" : "image",
                      "img-type" : "tile"
                    },
              legend : {
                      display : true,
                      textStyle : {fill : "black"},
                      draggable : true,
                      background: {
                        display : true,
                        style : {fill:"black",opacity:0.5}
                      }
                },
              line : {
                    colors: [{fill:"red"},{fill:"blue"},{fill:"green"},{fill:"yellow"}]
              },
              toolTip : {
                display: true,
                text : {
                  title : {fill:"black"},
                  message : {fill:"black"}
                },
                style : {stroke:"none",opacity:0.15}
              }
            };
Raphael.fn.lineChart = function(labels,values,config,theme){
  var r = this,
      set = r.set(),
      W = r.width,
      H = r.height,
      
      // The container :
      htitle = config.container.htitle,
      hfooter = config.container.hfooter,
      wleft = config.container.wleft,
      wright = config.container.wright,
      
      // The theme : (all parameters are Not Required)
      th = $.extend(true,{},Raphael.Chart.line.theme,theme);
      
      
  if(th.background.display){
    set.push(r.background(th.background));
  }    
  
  if(th.grid.display){
    set.push(r.grid({ystep:(H-htitle-hfooter)/11,xstep:(W-wleft-wright)/10,x:wleft,y:htitle,width:W-wleft-wright,height:H-htitle-hfooter},th.grid));
  }    
  
  if(th.xaxis.display){
    set.push(r.axis({x:wleft,width:W-wleft-wright,y:H-hfooter},th.xaxis));
  }    
  
  if(th.yaxis.display){
    set.push(r.axis({x:wleft,width:H-htitle-hfooter,y:H-hfooter,rotate:-90},th.yaxis));
  }
  
  var themeChart = {
    colors: th.colors,
    tooltip: 
  };
  
  return set;
}

Raphael.fn.lines = function(obj,theme){
    var r = this,
        set = r.set(),
        
        // Data
        categories = obj.xAxis.categories,
        series = obj.series,
        
        // Container : 
        width = obj.container.width,
        height = obj.container.height,
        x = obj.container.x,
        y = obj.container.y,
        
        // Variables temporaires :
        paths = Array(series.length),
        lines = Array(series.length),
        points = Array(series.length),
        xpas = width/categories.length;
        
   var max = 0,min = 0;
   for(var i = 0 ; i<series.length ; i++){
     Math.max.apply( Math, series[i].data)>max ? max = Math.max.apply( Math, series[i].data) : max = max;
     Math.min.apply( Math, series[i].data)<min ? min = Math.min.apply( Math, series[i].data) : min = min;
   }
   var temp = roundIt(min,max);
   max = temp.max;
   min = temp.min;
   set.info = temp;
   for(var i = 0 ; i<series.length ; i++){
     var x0 = xpas/2;
     paths[i] = [];
     lines[i] = r.set();
     points[i] = r.set();
     paths[i].push("M",x0,height-(series[i].data[0]-min)*height/(max-min));
     var point = r.regularPolygon(x0,height-(series[i].data[0]-min)*height/(max-min),5,i+3).attr(theme.colors[i]).attr({stroke:"none"});
     point.i = i;
     point.j = 0;
     lines[i].push(point);
     
     for(var j = 1 ; j<series[i].data.length ; j++){
       x0+=xpas;
       paths[i].push("L",x0,height-(series[i].data[j]-min)*height/(max-min));
       point = r.regularPolygon(x0,height-(series[i].data[j]-min)*height/(max-min),5,i+3).attr(theme.colors[i]).attr({stroke:"none"});
       point.i = i;
       point.j = j;
       points[i].push(point);
     }
     line = r.path().attr({
          path: paths[i].join(","),
            "stroke-width": 2,
            stroke:theme.colors[i].fill
        });
     line.i = i;
     points[i].toFront();
     lines[i].push(line,points[i]);
     
     //Tooltip : // The tooltip :
     var ttContainer = obj.tooltip;
      var theme1 = theme.tooltip;
      
      var tooltip = r.toolTip(ttContainer,theme1).hide();
     
     lines[i].hover(function(e){
       points[this.i].scale(1.25,1.25);
       lines[this.i].attr({"stroke-width":3});
       if(this.isPoint && theme.tooltip.display){
         var theme2 = {
            text : {
              title : {text:series[this.i].name},
              message : {text:categories[this.j]+" : "+series[this.i].data[this.j]}
            },
            style : {fill:theme.colors[this.i].fill}
          };
          theme2 = $.extend({},theme1,theme2);
          tooltip.show();
          tooltip.animateTo(this.x+x-ttContainer.width/2-10,this.y+y-ttContainer.height/2-10,theme2).toFront();
       }
     },
     function(e){
       points[this.i].scale(1,1);
       lines[this.i].attr({"stroke-width":2});
       if(this.isPoint){
         tooltip.hide();
       }
     });
     set.push(lines[i]);
   }
   set.translate(x,y);
   return set;
}


function roundMax(n){
  if(n<0){
    return -roundMin(Math.abs(n));
  }
  var res = Math.floor(Math.log(n)/Math.LN10);
  var temp = Math.floor(n/Math.pow(10,res));
  
  if(n == temp*Math.pow(10,res)){
    return n;
  }
  
  var x = (temp+0.5)*Math.pow(10,res);
  
  if(n<=x){
    return x;
  }
  
  return (temp+1)*Math.pow(10,res);
}

function roundMin(n){
  if(n<0){
    return -roundMax(Math.abs(n));
  }
  
  var res = Math.floor(Math.log(n)/Math.LN10);
  var temp = Math.floor(n/Math.pow(10,res));
  
  var x = (temp+0.5)*Math.pow(10,res);
  
  if(n>=x){
    return x;
  }
  
  return temp*Math.pow(10,res);
}

function roundIt(min,max){
  var tempMin = Math.abs(min);
  var tempMax = Math.abs(max);
  var res = {};
  
  if(Math.floor(Math.log(tempMin)/Math.LN10)<=Math.floor(Math.log(tempMax)/Math.LN10)){
    res.max = roundMax(max);
    res.step = Math.abs(res.max/10);
    var temp = res.max;
    while(temp>min){
      temp-=res.step;
    }
    res.min = roundMin(temp);
    return res;
  }
  res.min = roundMin(min);
  res.step = Math.abs(res.min/10);
  var temp = res.min;
  while(temp<max){
    temp+=res.step;
  }
  res.max = roundMax(temp);
  return res;
}